Ccay.Module.define("Ccay.Example.Ui.Gird2Igrid", function(page, $S) {
	page.main = function(gtGridOp) {

		var iGridOp, tempOp;
		
		// 替换除列以外的参数设置
		iGridOp = page.replaceOption(gtGridOp);
		
		// 获取列的参数设置
		tempOp = page.replaceColumnsOption(gtGridOp.columns);
		
		// 替换编辑模式
		tempOp.editMode && (iGridOp.editMode = true);
		
		// 替换列的参数设置
		iGridOp.columns = tempOp.columns;
		
		// 将对象转成Json字符串
		page.switchToJson(iGridOp);
		
	};
	
	/** @description 替换除列以外的参数设置
	 *  @param {Object} gtGridOp gtGrid的参数设置
	 */
	page.replaceOption = function (gtGridOp) {
		// 定义IGrid的参数Op
		var iGridOp = {}, width, height, minHeight, allowFreeze, customSetting, showSeq,
			autoLoad, remoteSort, sort, allowResize, noPage, editPermission, pagerOp,
			pageSize, curPage, totalRows, pageSizes, search, save, del, insert, fnComplete, 
			fnBeforeEdit, fnEndEdit;
		
		// width
		width = gtGridOp.width;
		width && (iGridOp.width = width);
		
		// height
		height = gtGridOp.height;
		iGridOp.height = height || 370;
		
		// minHeight
		minHeight = gtGridOp.minHeight;
		minHeight && (iGridOp.minHeight = minHeight);
		
		// allowFreeze
		allowFreeze = gtGridOp.allowFreeze;
		(allowFreeze != void 0) && (iGridOp.allowFreeze = allowFreeze);
		
		// allowPersonalized -> customSetting
		customSetting = gtGridOp.allowPersonalized;
		(customSetting != void 0) && (iGridOp.customSetting = customSetting);
		
		// showIndexColumn -> showSeq
		showSeq = gtGridOp.showIndexColumn;
		(showSeq != void 0) && (iGridOp.showSeq = showSeq);
		
		// autoLoad
		autoLoad = gtGridOp.autoLoad;
		(autoLoad != void 0) && (iGridOp.autoLoad = autoLoad);
		
		// remoteSort
		remoteSort = gtGridOp.remoteSort;
		(remoteSort != void 0) && (iGridOp.remoteSort = remoteSort);
		
		// sortInfo[fieldName, sortOrder, getSortValue] -> sort
		sort = gtGridOp.sortInfo;
		if (sort && sort.length > 0) {
			var sortObj = sort[0];
			iGridOp.sort = sortObj.fieldName + " " + sortObj.sortOrder;
		}
		
		// resizable -> allowResize
		allowResize = gtGridOp.resizable;
		(allowResize != void 0) && (iGridOp.allowResize = allowResize);
		
		// noPage -> showPager
		noPage = gtGridOp.noPage;
		(noPage != void 0) && (iGridOp.showPager = !noPage);
		
		// pagerOp
		pagerOp = gtGridOp.pagerOp;
		
		// pageSize
		pageSize = gtGridOp.pageSize;
		
		// pageNum -> curPage
		curPage = gtGridOp.pageNum;
		
		// totalRowNum -> totalRows
		totalRows = gtGridOp.totalRowNum;
		
		// pageSizeList -> pageSizes 
		pageSizes = gtGridOp.pageSizeList;
		
		if (pagerOp || pageSize || curPage || totalRows || pageSizes) {
			var pagerOp = pagerOp || {
				pageInf: {
					pageSize: pageSize || Ccay.UI.Config.pager.defaultSize,
					curPage: curPage || 1,
					totalRows: totalRows || 0
				},
				pageSizes: pageSizes || [].concat(Ccay.UI.Config.pager.pageSizes)
			};
			iGridOp.pagerOp = pagerOp;
		}
		
		// editPermission
		editPermission = gtGridOp.editPermission;
		editPermission && (iGridOp.editPermission = editPermission);
		
		// operation
		operation = gtGridOp.operation;
		
		// search
		search = operation && operation.search;
		
		// save 
		save = operation && operation.save;
		
		//del
		del = operation && operation.del;
		
		// add -> insert
		insert = gtGridOp.add;
		
		if (operation || search || save || del || insert) {
			var operation = {}, btn, fn;
			if (search) {
				var tempSearch = {}, ajax, panel, noLoading;
				// btn
				btn = search.btn;
				btn && (tempSearch.btn = btn);
				// panel
				panel = search.panel;
				panel && (tempSearch.panel = panel);
				// ajax -> iGridOp.ajax
				ajax = search.ajax || {};
				// noLoading -> iGridOp.ajax.loading
				noLoading = gtGridOp.noLoading;
				(noLoading != void 0) && (ajax.loading = !noLoading);
				// url -> iGridOp.ajax.url
				url = search.url;
				url && (ajax.url = url);
				// params -> iGridOp.ajax.params
				params = search.params;
				params && (ajax.params = params);
				// fnbefore -> iGridOp.ajax.setAjax
				fnbefore = search.fnbefore;
				fnbefore && (iGridOp.fnBeforeRequest = function (op) {fnbefore();});
				// fn -> iGridOp.ajax.setAjax
				fn = search.fn;
				fn && (iGridOp.fnBeforeRequest = function (op) {fn(); return false;});
				iGridOp.ajax = ajax;
				!$.isEmptyObject(tempSearch) && (operation.search = tempSearch);
			}
			if (save) {
				var tempSave = {}, ajax = {}, url;
				// btn
				btn = save.btn;
				btn && (tempSave.btn = btn);
				// url -> ajax.url
				url = save.url;
				url && (ajax.url = url);
				// fn -> ajax.setAjax
				fn = save.fn;
				fn && (ajax.setAjax = function (op, ui) {fn(gtGridOp, op.data); return false;});
				tempSave.ajax = ajax;
				operation.save = tempSave;
			}
			if (del) {
				var tempDel = {}, undoBtn;
				// btn 
				btn = del.btn;
				btn && (tempDel.btn = btn);
				// undoBtn
				undoBtn = del.undoBtn;
				undoBtn && (tempDel.undoBtn = undoBtn);
				operation.del = tempDel;
			}
			if (insert) {
				var tempInsert = {}, defaultRow;
				// btn
				btn = insert.btn;
				btn && (tempInsert.btn = btn);
				// defaultRecord -> defaultRow
				defaultRow = gtGridOp.defaultRecord;
				defaultRow && (tempInsert.defaultRow = defaultRow);
				operation.insert = tempInsert;
			}
			!$.isEmptyObject(operation) && (iGridOp.operation = operation);
		}
		
		// onComplete(grid) -> fnComplete()
		fnComplete = gtGridOp.onComplete;
		fnComplete && (iGridOp.fnComplete = fnComplete);
		
		// beforeEdit(value,  record,  col,  grid) -> fnBeforeEdit(value, td, col, rowData)
		fnBeforeEdit = gtGridOp.beforeEdit;
		fnBeforeEdit &&　(iGridOp.fnBeforeEdit = function (value, td, col, rowData) {fnBeforeEdit(value, rowData, col, this);});
		
		// afterEdit(value,  oldValue,  record,  col,  grid) -> fnEndEdit(value, rowData, td, col)
		fnEndEdit = gtGridOp.afterEdit;
		fnEndEdit && (iGridOp.fnEndEdit = function (value, rowData, td, col, oldValue) {fnEndEdit(value, oldValue, rowData, col, this);});
		
		return iGridOp;
	};
	
	/** @description 替换列的参数设置
	 *  @param {Object} gtGridColumnsOp gtGrid的列参数设置
	 */
	page.replaceColumnsOption = function (gtGridColumnsOp) {
		
		var tempOp = {}, columns = [], iGridColumn, editMode;
		
		if (gtGridColumnsOp && gtGridColumnsOp.length > 0) {
			$.each(gtGridColumnsOp, function (index, gtGridColumn) {
				
				// editor -> editMode
				gtGridColumn.editor && (editMode = true);
				iGridColumn = page.replaceSingleColumn(gtGridColumn);
				columns.push(iGridColumn);
			});
			editMode && (tempOp.editMode = true);
			tempOp.columns = columns;
		}
		return tempOp;
	};

	/** @description 替换单个列的参数设置
	 *  @param {Object} gtGridColumn gtGrid的列参数设置单个对象
	 */
	page.replaceSingleColumn = function (gtGridColumn) {
		// 定义IGrid的列参数Op
		var iGridColumn = {}, field, width, cssName, headCss, hidden, freezeable, header, sortable, sort,
			resizable, editable, permission, showTip, select, multi, format, type, rendererType, editorType, validRule;
		
		// isCheckColumn -> columnType: "select"
		select = gtGridColumn.isCheckColumn;
		if (select) {
			iGridColumn.columnType = "select";
			
			// multiSelect -> multi: true
			iGridColumn.multi = (gtGridColumn.multiSelect !== false) ? true : false;
		}
		
		// id -> field
		field = gtGridColumn.id;
		if (field && field != "chk" && field != "operation") {
			iGridColumn.field = field;
		} else if (field == "operation") {
			iGridColumn.columnType = "operate";
		}

		// width
		iGridColumn.width = gtGridColumn.width || 50;
			
		// styleClass/align -> cssName
		cssName = gtGridColumn.styleClass || gtGridColumn.align;
		cssName && (iGridColumn.cssName = cssName);
		
		// headAlign -> headCss 
		headCss = gtGridColumn.headAlign;
		headCss && (iGridColumn.headCss = headCss);
		
		// hidden
		hidden = gtGridColumn.hidden;
		(hidden != void 0) && (iGridColumn.hidden = hidden);
		
		// frozen -> freezeable
		freezeable = gtGridColumn.frozen;
		(freezeable != void 0) && (iGridColumn.freezeable = freezeable);
		
		// header/title -> header 
		header = gtGridColumn.header || gtGridColumn.title;
		header && (iGridColumn.header = header);
		
		// sortable
		sortable = gtGridColumn.sortable;
		(sortable != void 0) && (iGridColumn.sortable = sortable);
		
		// sortOrder -> sort 
		sort = gtGridColumn.sortOrder;
		sort && (iGridColumn.sort = sort);
		
		// resizable
		resizable = gtGridColumn.resizable;
		(resizable != void 0) && (iGridColumn.resizable = resizable);
		
		// editable
		editable = gtGridColumn.editable;
		(editable != void 0) && (iGridColumn.editable = editable);
		
		// ccayPermission -> permission
		permission = gtGridColumn.ccayPermission;
		permission && (iGridColumn.permission = permission);
		
		// toolTip -> showTip
		showTip = gtGridColumn.toolTip;
		(showTip != void 0) && (iGridColumn.showTip = showTip);
		
		// format -> format 
		format = gtGridColumn.format;
		!$.isEmptyObject(format) && page.changeType(format.type, format, iGridColumn);
		
		// type -> type
		type = gtGridColumn.type;
		type && page.changeType(type, {}, iGridColumn);
//		if (type) {
//			
//			(type == "valid" || type == "yesno" || type == "checkbox") && (iGridColumn.type = "bool");

//			(type == "language") && (iGridColumn.type = "// language类型，请手动转换!");

//		}
		
		// renderer -> renderer
		rendererType = gtGridColumn.renderer;
		if (rendererType && typeof(rendererType) == "function") {
			if (field == "operation") {
				iGridColumn.btns = "// 自定义的按钮，请手动转换！";
			} else {
				iGridColumn.renderer = "// 自定义的渲染器，请手动转换！";
			}
		}
		
		// editor -> editor
		editor = gtGridColumn.editor;
		!$.isEmptyObject(editor) && page.changeType(editor.type, editor, iGridColumn);
		
		// editor.validRule -> rule
		validRule = gtGridColumn.editor && gtGridColumn.editor.validRule;
		if (validRule && validRule.length > 0) {
			var tempRule = {}, isArray, len;
			$.each(validRule, function (i, rule) {
				isArray = $.isArray(rule);
				len = isArray && rule.length;
				tempRule = page.changeRule(tempRule, (isArray ? rule[0] : rule), 
					((len >= 2) ? rule[1] : null), ((len >= 3) ? rule[2] : null));
			});
			!$.isEmptyObject(tempRule) && (iGridColumn.rule = tempRule);
		}
		
		return iGridColumn;
	};
	
	/** @description 匹配gtgrid的验证规则
	 *  @param {Object} gtGridColumn gtGrid的列参数设置单个对象
	 */
	page.changeRule = function (validRule, rule, attr1, attr2) {
		(rule == "R" || rule == "required") && (validRule.required = true);
		(rule == "N" || rule == "number") && (validRule.number = true);
		(rule == "F" || rule == "float") && (validRule.number = true);
		(rule == "M" || rule == "money") && (validRule.number = true);
		(rule == "I" || rule == "integer" || rule == "int") && (validRule.digits = true);
		(rule == "E" || rule == "email") && (validRule.email = true);
		(rule == "U" || rule == "url") && (validRule.url = true);
		(rule == "D" || rule == "date") && (validRule.date = true);
		(rule == "DT" || rule == "datetime") && (validRule.dateISO = true);
		(rule == "MINL" || rule == "minlength") && (validRule.minlength = attr1);
		(rule == "MAXL" || rule == "maxlength") && (validRule.maxlength = attr1);
		(rule == "ELT") && (validRule.min = attr1);
		(rule == "EGT") && (validRule.max = attr1);
		(rule == "RG" || rule == "range") && (validRule.range = [attr1, attr2]);
		(rule == "DC" || rule == "dateCompare") && (validRule.dateCompare = true);
		(rule == "DCB" || rule == "dateCompareB") && (validRule.dateCompareB = true);
		(rule == "SPEC" || rule == "specialcharacter") && (validRule.specialch = true);
		(rule == "M" || rule == "money") && (validRule.number = true);
		return validRule;
	}

	page.changeType = function (type, typeObj, iGridColumn) {
		var editorOp = {}, format = {};
		if (type == "int") {
			iGridColumn.type = "integer";
		}
		if (type == "textarea") {
			iGridColumn.editor = "textarea";
			var width, height;
			width = typeObj.width;
			width && (editorOp.width = width);
			height = typeObj.height;
			height && (editorOp.height = height);
		}
		if (type == "select") {
			iGridColumn.type = "enum";
			var service;
			service = typeObj.options;
			service && (format.service = "// 请将options对象的数据，转换成[{value:0,text:''}]类型的数组！");
		}
		if (type == "lookup") {
			iGridColumn.type = "lookup";
			var code, supportLang;
			code = typeObj.code;
			code && (format.code = code);
			supportLang = typeObj.supportLang;
			supportLang && (format.supportLang = supportLang);
		}
		// 不完整
		if (type == "jcombo") {
			iGridColumn.type = "enum";
			var service, valueField, displayField;
			valueField = typeObj.valueField;
			valueField && (format.valueField = valueField);
			displayField = typeObj.displayField;
			displayField && (format.displayField = displayField);
			service = typeObj.service;
			if (service) {
				var dsconfig = service.split(":");
				if (dsconfig[0] == "REST") {
					editorOp.conf = {ajax : {url : dsconfig[1]}};
				} else {
					format.service = service;
				}
			}
		}
		if (type == "date" || type == "datetime") {
			iGridColumn.type = (type == "date") ? "date" : "datetime";
			var wmindate, wmaxdate, format;
			wmindate = typeObj.wmindate;
			wmindate && (editorOp.wmindate = wmindate);
			wmaxdate = typeObj.wmaxdate;
			wmaxdate && (editorOp.wmaxdate = wmaxdate);
			format = typeObj.format;
			format && (format.dateFormat = format);
		}
		if (type == "personInfo") {
			iGridColumn.renderer = "personInfo";
		}
		if (type == "groupInfo") {
			iGridColumn.renderer = "groupInfo";
		}
		if (type == "jldap") {
			iGridColumn.type = "ldap";
			var valueField, multi;
			valueField = typeObj.valueField;
			valueField && (format.valueField = valueField);
			multi = typeObj.multi;
			multi && (editorOp.conf = {multi: multi});
		}
		if (type == "jsuggest") {
			iGridColumn.type = "suggest";
			var conf, valueField, displayField, multi;
			conf = typeObj.suggestConf;
			conf && (editorOp.conf = "// jsuggest的配置信息，请手动转换！");
		}
		if (type == "jpop") {
			iGridColumn.type = "popup";
			var conf;
			conf = typeObj.popupBean;
			conf && (editorOp.conf = "// jpop的配置信息，请手动转换！");
		}
		if (type == "checkbox") {
			iGridColumn.type = "bool";
		}
		if (type == "language") {
			iGridColumn.type = "// language类型，请手动转换!";
		}
		if (type == "lang") {
			iGridColumn.type = "// lang类型，请手动转换!";
		}
		if (type == "yesno") {
			iGridColumn.type = "// yesno类型，请手动转换!";
		}
		if (type == "valid") {
			iGridColumn.type = "// valid类型，请手动转换!";
		}
		var callback;
		callback = typeObj.callback;
		callback && (editorOp = "// 该列存在callback方法，请手动转换！");
		!$.isEmptyObject(format) && (iGridColumn.format = format);
		!$.isEmptyObject(editorOp) && (iGridColumn.editorOp = editorOp);
	};
	
	/** @description 将对象转成Json字符串
	 *  @param {Object} gtGridColumn gtGrid的列参数设置单个对象
	 */
	page.switchToJson = function (iGridOp) {
		
		var iGridOpJson = Ccay.DataHelper.toJsonStr(iGridOp);
		
		iGridOpJson = iGridOpJson.replace("{", "{<br/>&nbsp;&nbsp;").replaceAll(",", ", <br/>&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll(":{"," : {<br/>&nbsp;&nbsp;&nbsp;&nbsp;").replaceAll(":",": ").replaceAll("},", "<br/>},");

		var editorBox = KindEditor.create('#testeditor', {
					width : '100%',
					height : '400px',
					afterChange : function() {
					}
				});

		editorBox.html(iGridOpJson);
	}
});